package com.kanas.webak.config.service;

import java.util.List;

import javax.inject.Singleton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kanas.framework.business.BusinessException;
import com.kanas.webak.config.domain.Properties;
import com.kanas.webak.config.domain.PropertiesGroup;
import com.kanas.webak.config.repository.PropertiesGroupRepository;
import com.kanas.webak.config.repository.PropertiesRepository;

@Service("com.kanas.webak.config.service.propertiesservice")
@Singleton
public class PropertiesServiceImpl implements  PropertiesService{
	
	@Autowired
	private PropertiesRepository propertiesRepository;

	@Autowired
	private PropertiesGroupRepository propertiesGroupRepository;
	
	@Override
	@Transactional
	public Properties create(Properties p, String groupCode) throws Exception {
		Properties pr = propertiesRepository.findOne(p.getKey());
		PropertiesGroup pg = propertiesGroupRepository.findOne(groupCode);
		if(pr!=null)throw BusinessException.DATA_DUPLICATION;
		if(pg==null)throw BusinessException.DATA_NOT_FOUND;
		Integer sort = propertiesRepository.findMaxSortNumber(groupCode);
		if(sort==null) sort=1;
		else sort++;
		p.setSortNumber(sort);
		p.setGroup(pg);
		propertiesRepository.save(p);
		return p;
	}
	
	@Override
	@Transactional
	public Properties update(Properties p) throws Exception {
		Properties pr = propertiesRepository.findOne(p.getKey());
		if(pr==null)throw BusinessException.DATA_NOT_FOUND;
		pr.setValue(p.getValue());
		pr.setType(p.getType());
		pr.setValidator(p.getValidator());
		propertiesRepository.save(pr);
		return p;
	}
	@Override
	public Properties updateValue(Properties p) throws Exception {
		Properties pr = propertiesRepository.findOne(p.getKey());
		if(pr==null)throw BusinessException.DATA_NOT_FOUND;
		pr.setValue(p.getValue());
		propertiesRepository.save(pr);
		return p;
	}

	@Override
	public String delete(Properties p) {
		propertiesRepository.delete(p);
		return p.getKey();
	}
	
	@Override
	@Transactional
	public Properties findOne(String id) {
		Properties dt = propertiesRepository.findOne(id);
		return dt;
	}
	
	@Override
	@Transactional
	public List<Properties> findByGroup(String groupCode) {
		return propertiesRepository.findByGroupCode(groupCode);
	}

	@Override
	public List<PropertiesGroup> findGroups() {
		Sort sort = new Sort(new Order(Direction.ASC,"sortNumber"));
		return propertiesGroupRepository.findAll(sort);
	}

	@Override
	public PropertiesGroup createGroup(PropertiesGroup g) throws Exception {
		PropertiesGroup pg = propertiesGroupRepository.findOne(g.getCode());
		if(pg!=null) throw BusinessException.DATA_DUPLICATION;
		Integer sn = propertiesGroupRepository.findMaxGroupSortNumber();
		g.setSortNumber(sn==null?1:sn+1);
		propertiesGroupRepository.save(g);
		return g;
	}

	public PropertiesRepository getPropertiesRepository() {
		return propertiesRepository;
	}

	public void setPropertiesRepository(PropertiesRepository propertiesRepository) {
		this.propertiesRepository = propertiesRepository;
	}

	public PropertiesGroupRepository getPropertiesGroupRepository() {
		return propertiesGroupRepository;
	}

	public void setPropertiesGroupRepository(
			PropertiesGroupRepository propertiesGroupRepository) {
		this.propertiesGroupRepository = propertiesGroupRepository;
	}

}
